package mysgoog.bot.algo;

import java.util.Arrays;
import java.util.Vector;

import mysgoog.board.Board;
import mysgoog.gtp.Color;

public class Occupy {

	private static final int BD_LEN = 13;
	private static final int MAX_SIZE = 13 * 13;
	private static boolean[] flag = new boolean[MAX_SIZE];

	private static int[] testX = { 3, 3, -3, -3, 2, 2, -2, -2 };
	private static int[] testY = { 2, -2, 2, -2, 3, -3, 3, -3 };
	private static int[] moveX = { 2, 2, -2, -2, 1, 1, -1, -1 };
	private static int[] moveY = { 1, -1, 1, -1, 2, -2, 2, -2 };

	public static Vector<Integer> occupy(Board board, Color color) {
		Vector<Integer> res = new Vector<Integer>();

		Arrays.fill(flag, false);

		for (int i = 0; i < BD_LEN; ++i) {
			for (int j = 0; j < BD_LEN; ++j) {
				int org = board.makePos(i, j);
				if (board.getStone(org) == color) {

					for (int k1 = 0; k1 < testX.length; ++k1) {
						for (int k2 = 0; k2 < testY.length; ++k2) {
							int topos = board.makePos(i + testX[k1], j
									+ testY[k2]);
							if (board.isLegal(color, topos)) {
								boolean go = true;

								int begx = Math.min(i, i + testX[k1]);
								int endx = Math.max(i, i + testX[k1]);
								int begy = Math.min(j, j + testY[k2]);
								int endy = Math.max(j, j + testY[k2]);

								for (int k3 = begx; k3 < endx; ++k3) {
									for (int k4 = begy; k4 < endy; ++k4) {
										int scanpos = board.makePos(k3, k4);
										if (scanpos != org
												&& board.getStone(scanpos) != Color.EMPTY) {
											go = false;
											break;
										}
									}
									if (!go) {
										break;
									}
								}
								if (go && i + testX[k1] != 0
										&& i + testX[k1] != 12
										&& j + testY[k2] != 0
										&& j + testY[k2] != 12) {
									flag[board.makePos(i + moveX[k1], j
											+ moveY[k2])] = true;
								}

							}
						}
					}
				}
			}
		}

		for (int i = 0; i < flag.length; ++i) {
			if (flag[i]) {
				res.add(i);
			}
		}
		return res;
	}

}
